﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Configuration;
using System.Configuration;
using System.Web;
using System.Xml;

namespace Marbles.Core.Settings
{
    /// <summary>
    /// Provides a frame to store Database Connection Settings.
    /// </summary>
    public class DataConnectionSettings
    {

        public string ProviderName { get { return _providername; } set { _providername = value; } }
        public string Host { get { return _host; } set { _host = value; } }
        public string Schema { get { return _schema; } set { _schema = value; } }
        public string Username { get { return _username; } set { _username = value; } }
        public string Password { get { return _password; } set { _password = value; } }

        private string _providername = string.Empty;
        private string _host = string.Empty;
        private string _schema = string.Empty;
        private string _username = string.Empty;
        private string _password = string.Empty;

        /// <summary>
        /// Constructor
        /// </summary>
        public DataConnectionSettings()
        {
            if (WebConfigurationManager.ConnectionStrings["Default"] == null) { return; }

            _providername = WebConfigurationManager.ConnectionStrings["Default"].ProviderName.ToLower();
            string[] dsn = WebConfigurationManager.ConnectionStrings["Default"].ConnectionString.Split(';');

            foreach (string s in dsn)
            {
                string[] value = s.Split('=');
                switch (value[0].ToUpper())
                {
                    case "SERVER":
                        _host = value[1]; break;
                    case "DATABASE":
                        _schema = value[1]; break;
                    case "USER ID":
                    case "UID":
                        _username = value[1]; break;
                    case "PWD":
                        _password = value[1];
                        break;

                }
            }
        }

        internal static void UpdateDataConnectionSettings(DataConnectionSettings value)
        {
            XmlDocument xmlDoc = new XmlDocument();
            string filename = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath + "/web.config");
            xmlDoc.Load(filename);
            XmlElement root = xmlDoc.DocumentElement;
            XmlNode node = root.SelectSingleNode("//configuration/connectionStrings/add[@name='Default']");

            if (node != null)
            {
                node.Attributes["connectionString"].Value = string.Format("SERVER={0};DATABASE={1};USER ID={2};PWD={3}", value.Host, value.Schema, value.Username, value.Password);
                node.Attributes["providerName"].Value = value.ProviderName.ToLower();
            }
            else
            {
                node = xmlDoc.CreateElement("add");
                XmlAttribute nKey = xmlDoc.CreateAttribute("name");
                XmlAttribute nProvider = xmlDoc.CreateAttribute("providerName");
                XmlAttribute nConnection = xmlDoc.CreateAttribute("connectionString");
                nKey.Value = "Default";
                nProvider.Value = value.ProviderName.ToLower();
                nConnection.Value = string.Format("SERVER={0};DATABASE={1};USER ID={2};PWD={3}", value.Host, value.Schema, value.Username, value.Password);

                node.Attributes.Append(nKey);
                node.Attributes.Append(nProvider);
                node.Attributes.Append(nConnection);
                root.SelectSingleNode("//configuration/connectionStrings").AppendChild(node);
            }

            xmlDoc.Save(filename);
        }

    }
}
